package com.clstu.reader_;

import org.testng.annotations.Test;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class FileReader_ {
    public static void main(String[] args) {

    }

    @Test
    public void readFile(){

        FileReader fileReader=null;

        try {
            fileReader = new FileReader("D:\\hello.txt");//字符输入流
            int data = 0;
            while ((data=fileReader.read())!=-1){//安装单个字符读入，
                System.out.print((char) data);
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();//close()之后如果有改变才能写回到磁盘中去，不然它只在内存中
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void readFile02(){
        System.out.println("============readFile02---------");
        FileReader fileReader=null;
        int readLen=0;
        char[] buf = new char[8];//假定每次最多读取8个字符

        try {
            fileReader = new FileReader("D:\\hello.txt");//字符输入流
            int data = 0;
            while ((readLen=fileReader.read(buf))!=-1){//按照多个字符读入，
                System.out.print(new String(buf,0,readLen));
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileReader.close();//close()之后如果有改变才能写回到磁盘中去，不然它只在内存中
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
